<template>
	<view>
		<view class="container" v-if="usershow">
			<view class="nav">
				<view class="nav-item" :class="{active: currentNav === 'all'}" @click="switchTab('all')">全部订单
				</view>
				<view class="nav-item" :class="{active: currentNav === 'pending'}" @click="switchTab('pending')">待处理
				</view>
				<view class="nav-item" :class="{active: currentNav === 'processing'}" @click="switchTab('processing') ">
					处理中
				</view>
				<view class="nav-item" :class="{active: currentNav === 'completed'}" @click="switchTab('completed')">已完成
				</view>

			</view>

			<view class="order-list">
				<view v-for="order in orderList" :key="order.orderId" @click="navigateToOrderDetail(order.orderId)">
					<view class="order-item" v-if="order">
						<view class="order-status">
							<span v-if="order.orderProcess === 0">待处理</span>
							<span v-else-if="order.orderProcess === 1">处理中</span>
							<span v-else-if="order.orderProcess === 2">已完成</span>
						</view>
						<view class="order-info">
							<!-- <view>订单类型：{{order.type}}</view> -->
							<view style="height: 11px;"></view>
							<view>订单编号：{{order.orderId}}</view>

							<!-- <view>报修区域：{{order.area}}</view> -->
							<view>报修时间：{{order.orderTime}}</view>
						</view>
					</view>
				</view>
			</view>

		</view>
		<view class="container" v-if="workshow">
			<view class="nav">
				<view class="nav-item" :class="{active: currentNav === 'all'}" @click="switchTab2('all')">(工人)全部订单
				</view>
				<view class="nav-item" :class="{active: currentNav === 'pending'}" @click="switchTab2('pending')">待处理
				</view>
				<view class="nav-item" :class="{active: currentNav === 'processing'}"
					@click="switchTab2('processing') ">
					处理中
				</view>
				<view class="nav-item" :class="{active: currentNav === 'completed'}" @click="switchTab2('completed')">
					已完成
				</view>

			</view>

			<view class="order-list">
				<view v-for="order in orderList" :key="order.orderId" @click="navigateToOrderDetail(order.orderId)">
					<view class="order-item" v-if="order">
						<view class="order-status">
							<span v-if="order.orderProcess === 0">待处理</span>
							<span v-else-if="order.orderProcess === 1">处理中</span>
							<span v-else-if="order.orderProcess === 2">已完成</span>
						</view>
						<view class="order-info">
							<!-- <view>订单类型：{{order.type}}</view> -->
							<view style="height: 11px;"></view>
							<view>订单编号：{{order.orderId}}</view>

							<!-- <view>报修区域：{{order.area}}</view> -->
							<view>报修时间：{{order.orderTime}}</view>
							<button v-if="order.orderProcess === 0" @click="changeOrderProcess(order)">点击接单 </button>
						</view>
					</view>
				</view>
			</view>

		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				workshow: '',
				usershow: '',
				currentNav: 'all', // 当前选中的导航
				tel: '15355635875',
				jobNumber: '',
				orders: [
				],
				orderList: [
				],
			};
		},
		computed: {},
		onLoad() {
			
			
		},
		onShow(){
			this.jobNumber = this.$store.getters.getJobNumber;
			this.usershow = this.$store.getters.getusershow;
			this.workshow = this.$store.getters.getworkshow;
			console.log(this.usershow);
			if (this.userShow) { // 如果 usershow 为 true
				uni.request({
					url: 'http://braincognitiontec.top:30003/order/get/byTel?tel=' + this.tel,
					method: 'GET',
					success: (res) => {
						this.allOrders = res.data.data;
						console.log(this.allOrders)
						this.orderList = this.allOrders;
					},
					fail: (res) => {
						console.log(res)
					}
				});
			} else { // 如果 usershow 为 false
				uni.request({
					url: 'http://braincognitiontec.top:30003/employee/get-AllOrder/byEmployeeIdAndNotProcessing?employeeId=' +
						this.$store.getters.getJobNumber,
					method: 'GET',
					success: (res) => {
						this.allOrders = res.data.data;
						console.log(this.allOrders)
						this.orderList = this.allOrders;
					},
					fail: (res) => {
						console.log(res)
					}
				});
			}
		},
		methods: {

			switchTab(tab) {
				// 切换 tab
				this.currentNav = tab;
				switch (tab) {
					case 'all':
						uni.request({
							url: 'http://braincognitiontec.top:30003/order/get/byTel?tel=' + this.tel,
							method: 'GET',
							success: (res) => {
								this.allOrders = res.data.data;
								console.log(this.allOrders)
								// this.pendingOrders = this.allOrders.filter((item) => item.status === '待处理');
								// this.processingOrders = this.allOrders.filter((item) => item.status === '处理中');
								// this.completedOrders = this.allOrders.filter((item) => item.status === '已完成');
								// this.rejectedOrders = this.allOrders.filter((item) => item.status === '驳回');
								this.orderList = this.allOrders;
							},
							fail: (res) => {
								console.log(res)
							}
						});
						this.currentNav = tab;
						break;
					case 'pending':
						uni.request({
							url: 'http://braincognitiontec.top:30003/order/get/byTel/byOrderProcess?tel=' + this
								.tel + '&orderProcess=0',
							method: 'GET',
							success: (res) => {
								this.orderList = res.data.data;
								console.log(this.orderList)

							},
							fail: (res) => {
								console.log(res)
							}
						});
						this.orderList = this.pendingOrders;
						this.currentNav = tab;
						break;
					case 'processing':
						uni.request({
							url: 'http://braincognitiontec.top:30003/order/get/byTel/byOrderProcess?tel=' + this
								.tel + '&orderProcess=1',
							method: 'GET',
							success: (res) => {
								this.orderList = res.data.data;
								console.log(this.orderList)

							},
							fail: (res) => {
								console.log(res)
							}
						});
						this.orderList = this.processingOrders;
						this.currentNav = tab;
						break;
					case 'completed':
						uni.request({
							url: 'http://braincognitiontec.top:30003/order/get/byTel/byOrderProcess?tel=' + this
								.tel + '&orderProcess=2',
							method: 'GET',
							success: (res) => {
								this.orderList = res.data.data;
								console.log(this.orderList)

							},
							fail: (res) => {
								console.log(res)
							}
						});
						this.orderList = this.completedOrders;
						this.currentNav = tab;
						break;
					case 'rejected':
						uni.request({
							url: 'http://braincognitiontec.top:30003/order/get/byTel/byOrderProcess?tel=' + this
								.tel + '&orderProcess=-1',
							method: 'GET',
							success: (res) => {
								this.orderList = res.data.data;
								console.log(this.orderList)


							},
							fail: (res) => {
								console.log(res)
							}
						});
						this.orderList = this.rejectedOrders;
						this.currentNav = tab;
						break;
					default:
						break;
				}
			},

			changeOrderProcess(order) {
				
				uni.request({
					url: 'http://braincognitiontec.top:30003/order/updateOrderProcess/toEmployee',
					method: 'POST',
					header: {
						'Content-Type': 'application/x-www-form-urlencoded',
					},
					data: {
						orderId: order.orderId,
						employeeId: this.jobNumber,
					},
					success: (res) => {

						console.log(res.data.success)
						console.log(res.data.data)
					},
					fail: (err) => {
						console.log(err)
					}
				})

				console.log('id', this.jobNumber);
				console.log('用户点击的订单编号：', order.orderId);
			},

			switchTab2(tab) {
				// 切换 tab
				this.currentNav = tab;
				switch (tab) {
					case 'all':
						uni.request({
							url: 'http://braincognitiontec.top:30003/employee/get-AllOrder/byEmployeeIdAndNotProcessing?employeeId=' +
								this.jobNumber,
							method: 'GET',
							success: (res) => {
								this.allOrders = res.data.data;
								console.log(this.allOrders)
								// this.pendingOrders = this.allOrders.filter((item) => item.status === '待处理');
								// this.processingOrders = this.allOrders.filter((item) => item.status === '处理中');
								// this.completedOrders = this.allOrders.filter((item) => item.status === '已完成');
								// this.rejectedOrders = this.allOrders.filter((item) => item.status === '驳回');
								this.orderList = this.allOrders;
							},
							fail: (res) => {
								console.log(res)
							}
						});
						this.currentNav = tab;
						break;
					case 'pending':
						uni.request({
							url: 'http://braincognitiontec.top:30003/employee/get-Order/notProcessing',
							method: 'GET',
							success: (res) => {
								this.orderList = res.data.data;
								console.log(this.orderList)

							},
							fail: (res) => {
								console.log(res)
							}
						});
						this.orderList = this.pendingOrders;
						this.currentNav = tab;
						break;
					case 'processing':
						uni.request({
							url: 'http://braincognitiontec.top:30003/employee/get-processingOrder/byEmployee?employeeId=' +
								this.jobNumber,
							method: 'GET',
							success: (res) => {
								this.orderList = res.data.data;
								console.log(this.orderList)

							},
							fail: (res) => {
								console.log(res)
							}
						});
						this.orderList = this.processingOrders;
						this.currentNav = tab;
						break;
					case 'completed':
						uni.request({
							url: 'http://braincognitiontec.top:30003/employee/get-completedOrder/byEmployee?employeeId=' +
								this.jobNumber,
							method: 'GET',
							success: (res) => {
								this.orderList = res.data.data;
								console.log(this.orderList)

							},
							fail: (res) => {
								console.log(res)
							}
						});
						this.orderList = this.completedOrders;
						this.currentNav = tab;
						break;
					case 'rejected':
						uni.request({
							url: 'http://braincognitiontec.top:30003/order/get/byTel/byOrderProcess?tel=' + this
								.tel + '&orderProcess=-1',
							method: 'GET',
							success: (res) => {
								this.orderList = res.data.data;
								console.log(this.orderList)


							},
							fail: (res) => {
								console.log(res)
							}
						});
						this.orderList = this.rejectedOrders;
						this.currentNav = tab;
						break;
					default:
						break;
				}
			},


			navigateToOrderDetail(orderId) {
				uni.navigateTo({
					url: '/pages/order-detail/order-detail?orderId=' + orderId
				})
			},


		},
	};
</script>

<style>
	/* 之前给出的样式代码 */
	.container {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.nav {
		display: flex;
		justify-content: space-around;
		width: 100%;
		height: 50px;
		line-height: 50px;
		background-color: #f2f2f2;
	}

	.nav-item {
		font-size: 16px;
		color: #333;
	}

	.nav-item.active {
		color: #007aff;
		border-bottom: 2px solid #007aff;
	}


	.order-list {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		padding-top: 20px;
	}

	.order-item {
		display: flex;
		flex-direction: column;
		margin-bottom: 20px;
		padding: 10px;
		border: 1px solid #ccc;
	}

	.order-status {
		position: relative;
	}

	.order-status span {
		position: absolute;
		top: 0;
		right: 0;
		color: red;
		font-weight: bold;
	}

	.order-info {

		display: flex;
		flex-direction: column;
	}

	.order-info view {
		margin-bottom: 5px;
	}
</style>